/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean;

import com.jb.web.context.WebContext;
import com.jb.web.model.css.Style;

import java.util.List;

import com.jb.faces.component.NamingContainer;

import com.jb.web.bean.inspector.OptionPropertyEditor;

import java.util.Iterator;
import java.util.Map;


public class Region extends Text implements NamingContainer {
    static final long serialVersionUID = 123123L;

    static {
        publish(Region.class, STYLE, "overFlow", 
                new OptionPropertyEditor("static:,Visible;hidden,Hidden;auto,Auto;scroll,Scroll;"));
        publish(Region.class, STYLE, "stretch", 
                new OptionPropertyEditor("static:,None;vertical,Vertical;horizontal,Horizontal;both,Both;"));
        publish(com.jb.web.bean.Region.class, STYLE, "opacity");
    }
    private String stretch;
    private Label header;

    private String overFlow;
    private int opacity;

    public void setStretch(String param) {
        this.stretch = param;
    }

    public String getStretch() {
        return stretch;
    }


    public String getDimensionStyle() {
        return super.getDimensionStyle() + Style.addProperty("overflow", overFlow, null);
    }

    public void setOpacity(int opacity) {
        this.opacity = opacity;
    }

    public int getOpacity() {
        return opacity;
    }


    public void setOverFlow(String overFlow) {
        this.overFlow = overFlow;
    }

    /**
     * Returns overflow style.
     *
     * @return overflow style.
     */
    public String getOverFlow() {
        return overFlow;
    }
    
    public List getRegions() {
        return getChildrenByType(com.jb.web.bean.Region.class);
    }
    
    /**
     * Return style string.
     * @param ctx the runtime context.
     * @return CSS style for this component.
     */
    public String getStyle(WebContext ctx) {
        String style = super.getStyle(ctx);

        if (opacity > 0) {
            style = style + Style.addProperty("opacity", "" + ((100f - opacity) / 100), null) +
                Style.addProperty("filter", "alpha(opacity=" + (100 - opacity) + ")", null);
        }

        return style;
    }
    public Label getHeader() {
        if (header == null) {
            Map map = getFacets();
            Iterator iter = map.values().iterator();

            while (iter.hasNext()) {
                Object facet = iter.next();

                if (facet instanceof Label) {
                    header = (Label) facet;
                }
            }
        }

        if (header == null) {
            createHeader();
        }

        if (header.getId() == null) {
            header.setId(getHeaderId());
        }

        return header;
    }
    void createHeader() {
        if (getFacets() != null) {
            getFacets().clear();
        }

        header = null;

        if (header == null) {
            header = new Label();
            header.setId(getHeaderId());
        }

        if (header.getValue() == null) {
            header.setValue(getValue());
        }

       // header.setStyleClass("jbTab");
        header.setFacet(true);
        addFacet(getHeaderId(), header);
    }
    /**
     * Copies attributes from another component.
     *
     * @param uiComponent to be assigned to
     */
    public void assign(BaseComponent uiComponent) {
        super.assign(uiComponent);

        Region p = (Region) uiComponent;

        if ((header != null) && (p.getHeader() != null)) {
            header.assign(p.getHeader());
            header.setId(getHeaderId());
        }
    }

    private String getHeaderId() {
        return getId() + "Header";
    }
    
}
